/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.data.common.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.FastDateFormat;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 日期工具类, 继承org.apache.commons.lang.time.DateUtils类
 * @author ThinkGem
 * @version 2017-1-4
 */
public class MyDateUtils extends org.apache.commons.lang3.time.DateUtils {

	private static String[] parsePatterns = {
		"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd HH", "yyyy-MM",
		"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM/dd HH", "yyyy/MM",
		"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM.dd HH", "yyyy.MM",
		"yyyy年MM月dd日", "yyyy年MM月dd日 HH时mm分ss秒", "yyyy年MM月dd日 HH时mm分", "yyyy年MM月dd日 HH时", "yyyy年MM月",
		"yyyy","yyyyMM"};

	/**
	 * 得到日期字符串 ，转换格式（yyyy-MM-dd）
	 */
	public static String formatDate(Date date) {
		return formatDate(date, "yyyy-MM-dd");
	}

	/**
	 * 得到日期字符串 ，转换格式（yyyyMMdd）
	 */
	public static String formatDateStr(Date date) {
		return formatDate(date, "yyyyMMdd");
	}
	/**
	 * 得到日期字符串 ，转换格式（yy-MM-dd）
	 */
	public static String formatDate_(Date date) {
		return formatDate(date, "yy.MM.dd");
	}

	/**
	 * 得到日期字符串 默认格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String formatDate(long dateTime, String pattern) {
		return formatDate(new Date(dateTime), pattern);
	}

	/**
	 * 得到日期字符串 默认格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String formatDate(Date date, String pattern) {
		String formatDate = null;
		if (date != null){
//			if (StringUtils.isNotBlank(pattern)) {
//				formatDate = DateFormatUtils.format(date, pattern);
//			} else {
//				formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
//			}
			if (StringUtils.isBlank(pattern)) {
				pattern = "yyyy-MM-dd";
			}
			formatDate = FastDateFormat.getInstance(pattern).format(date);
		}
		return formatDate;
	}

	/**
	 * 得到日期时间字符串，转换格式（yyyy-MM-dd HH:mm:ss）
	 */
	public static String formatDateTime(Date date) {
		return formatDate(date, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 等到指定日期字符串，格式是月
	 * */
	public static String formatDateTimeByMonth(Date date) {
		return formatDate(date, "M");
	}
	public static String formatDateTimeByDay(Date date) {
		return formatDate(date, "d");
	}
	public static String formatDateTimeByHour(Date date) {
		return formatDate(date, "H");
	}
	public static String formatDateTimeByMin(Date date) {
		return formatDate(date, "m");
	}

	/**
	 * 得到当前日期字符串 格式（yyyy-MM-dd）
	 */
	public static String getDate() {
		return getDate("yyyy-MM-dd");
	}

	/**
	 * 得到当前日期字符串 格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String getDate(String pattern) {
//		return DateFormatUtils.format(new Date(), pattern);
		return FastDateFormat.getInstance(pattern).format(new Date());
	}

	/**
	 * 得到当前日期前后多少天，月，年的日期字符串
	 * @param pattern 格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	 * @param amont 数量，前为负数，后为正数
	 * @param type 类型，可参考Calendar的常量(如：Calendar.HOUR、Calendar.MINUTE、Calendar.SECOND)
	 * @return
	 */
	public static String getDate(String pattern, int amont, int type) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(type, amont);
//		return DateFormatUtils.format(calendar.getTime(), pattern);
		return FastDateFormat.getInstance(pattern).format(calendar.getTime());
	}

	/**
	 * 得到指定日期前后多少天，月，年的日期字符串
	 * @param pattern 格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	 * @param amont 数量，前为负数，后为正数
	 * @param type 类型，可参考Calendar的常量(如：Calendar.HOUR、Calendar.MINUTE、Calendar.SECOND)
	 * @return
	 */
	public static String getDate(Date date,String pattern, int amont, int type) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(type, amont);
//		return DateFormatUtils.format(calendar.getTime(), pattern);
		return FastDateFormat.getInstance(pattern).format(calendar.getTime());
	}

	/**
	 * 得到当前时间字符串 格式（HH:mm:ss）
	 */
	public static String getTime() {
		return formatDate(new Date(), "HH:mm:ss");
	}

	public static String getTime(Date date) {
		return formatDate(date, "HH:mm:ss");
	}

	/**
	 * 得到当前日期和时间字符串 格式（yyyy-MM-dd HH:mm:ss）
	 */
	public static String getDateTime() {
		return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 得到当前年份字符串 格式（yyyy）
	 */
	public static String getYear() {
		return formatDate(new Date(), "yyyy");
	}

	/**
	 * 得到当前月份字符串 格式（MM）
	 */
	public static String getMonth() {
		return formatDate(new Date(), "MM");
	}

	/**
	 * 得到当天字符串 格式（dd）
	 */
	public static String getDay() {
		return formatDate(new Date(), "dd");
	}
	/**
	 * 得到当天字符串 格式（HH）
	 * */
	public static String getHours() {
		return formatDate(new Date(), "HH");
	}
	/**
	 * 得到当天字符串 格式（mm）
	 * */
	public static String getMinute() {
		return formatDate(new Date(), "mm");
	}

	/**
	 * 得到当前星期字符串 格式（E）星期几
	 */
	public static String getWeek() {
		return formatDate(new Date(), "E");
	}

	/**
	 * 日期型字符串转化为日期 格式   see to DateUtils#parsePatterns
	 */
	public static Date parseDate(Object str) {
		if (str == null){
			return null;
		}
		try {
			return parseDate(str.toString(), parsePatterns);
		} catch (ParseException e) {
			return null;
		}
	}

	public static Date parseDateNew(Object str) {
		if (str == null){
			return null;
		}
		try {
			return parseDate(str.toString(), "yyyyMM");
		} catch (ParseException e) {
			return null;
		}
	}

	public static Date parseNullDate(Object str) {
		if (str == null || ("").equals(str)){
			return null;
		}
		try {
			return parseDate(str.toString(), parsePatterns);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 获取过去的天数
	 * @param date
	 * @return
	 */
	public static long pastDays(Date date) {
		long t = System.currentTimeMillis()-date.getTime();
		return t/(24*60*60*1000);
	}

	/**
	 * 获取过去的小时
	 * @param date
	 * @return
	 */
	public static long pastHour(Date date) {
		long t = System.currentTimeMillis()-date.getTime();
		return t/(60*60*1000);
	}

	/**
	 * 获取过去的分钟
	 * @param date
	 * @return
	 */
	public static long pastMinutes(Date date) {
		long t = System.currentTimeMillis()-date.getTime();
		return t/(60*1000);
	}

	/**
	 * 两个日期之间的多少天
	 *
	 * @param date1 the date 1
	 * @param date2 the date 2
	 * @return 前小后大 为正数 前大后小 为负数
	 */
	public static int getBetweenDay(Date date1, Date date2) {
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date1);
		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(date2);
		int day1 = cal1.get(Calendar.DAY_OF_YEAR);
		int day2 = cal2.get(Calendar.DAY_OF_YEAR);
		int year1 = cal1.get(Calendar.YEAR);
		int year2 = cal2.get(Calendar.YEAR);

		int timeDistance = 0;
		//不同一年
		if (year1 != year2) {
			int smallYear = year1;
			int bigYear = year2;
			if (smallYear > bigYear) {
				int temp = smallYear;
				smallYear = bigYear;
				bigYear = temp;
			}
			for (int i = smallYear; i < bigYear; i++) {
				//闰年
				if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
					timeDistance += 366;
				} else {
					timeDistance += 365;
				}
			}
			if (year1 > year2) {
				timeDistance = -timeDistance;
			}
		}
		return timeDistance + (day2 - day1);
	}
	/**
	 * 获取两个日期之间的天数
	 *
	 * @param before
	 * @param after
	 * @return
	 */
	public static double getDistanceOfTwoDate(Date before, Date after) {
		long beforeTime = before.getTime();
		long afterTime = after.getTime();
		return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
	}
	/**
	 * 获取两个日期之间的小时数
	 *
	 * @param before
	 * @param after
	 * @return
	 */
	public static double getHourOfTwoDate(Date before, Date after) {
		long beforeTime = before.getTime();
		long afterTime = after.getTime();
		return (afterTime - beforeTime) / (1000 * 60 * 60);
	}

	//获取月集合
	public static List<Date> getAllTheDateOftheMonth(Date date) {
		List<Date> list = new ArrayList<Date>();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE, 1);

		int month = cal.get(Calendar.MONTH);
		while(cal.get(Calendar.MONTH) == month){
			list.add(cal.getTime());
			cal.add(Calendar.DATE, 1);
		}
		return list;
	}
	/**
	 * 获取两个日期之间的分钟数
	 *
	 * @param before
	 * @param after
	 * @return
	 */
	public static double getMinutesOfTwoDate(Date before, Date after) {
		long beforeTime = before.getTime();
		long afterTime = after.getTime();
        return (afterTime - beforeTime) / (1000 * 60);
    }

	/**
	 * 获取某月有几天
	 * @param date 日期
	 * @return 天数
	 */
	public static int getMonthHasDays(Date date){
//		String yyyyMM = new SimpleDateFormat("yyyyMM").format(date);
		String yyyyMM = FastDateFormat.getInstance("yyyyMM").format(date);
		String year = yyyyMM.substring(0, 4);
		String month = yyyyMM.substring(4, 6);
		String day31 = ",01,03,05,07,08,10,12,";
		String day30 = "04,06,09,11";
		int day = 0;
		if (day31.contains(month)) {
			day = 31;
		} else if (day30.contains(month)) {
			day = 30;
		} else {
			int y = Integer.parseInt(year);
			if ((y % 4 == 0 && (y % 100 != 0)) || y % 400 == 0) {
				day = 29;
			} else {
				day = 28;
			}
		}
		return day;
	}

	/**
	 * 获取日期是当年的第几周
	 * @param date
	 * @return
	 */
	public static int getWeekOfYear(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.WEEK_OF_YEAR);
	}

	/**
	 * 获取一天的开始时间（如：2015-11-3 00:00:00.000）
	 * @param date 日期
	 * @return
	 */

//	public static Date getOfDayFirst(Date date) {
//		if (date == null){
//			return null;
//		}
//		long current=date.getTime ();//当前时间毫秒数
//		long zero=current/(1000*3600*24)*(1000*3600*24)- TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
//		return new Date (zero);
//	}

	public static Date getOfDayFirst(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime ();
	}


	/**
	 * 获取一天的最后时间（如：2015-11-3 23:59:59.999）
	 * @param date 日期
	 * @return
	 */
//	public static Date getOfDayLast(Date date) {
//		if (date == null){
//			return null;
//		}
//		long current=date.getTime ();
//		long zero=current/(1000*3600*24)*(1000*3600*24)- TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
//		long twelve=zero+24*60*60*1000-1;//今天23点59分59秒的毫秒数
//		return new Date (twelve);
//	}
	public static Date getOfDayLast(Date date) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.set(Calendar.HOUR_OF_DAY, 23);
			calendar.set(Calendar.MINUTE, 59);
			calendar.set(Calendar.SECOND, 59);
            calendar.set(Calendar.MILLISECOND, 999);
			return calendar.getTime ();
	}
	public static Date getDayLast(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		return calendar.getTime ();
	}

	/**
	 * 获取服务器启动时间
	 * @param
	 * @return
	 */
	public static Date getServerStartDate(){
		long time = ManagementFactory.getRuntimeMXBean().getStartTime();
		return new Date(time);
	}



	public static String dateToString(Date date) {
		Calendar c1 = Calendar.getInstance();
		c1.setTime(new Date());
		SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
		Calendar c2 = Calendar.getInstance();
		c2.setTime(date);
		Long dValue = c1.getTimeInMillis() - c2.getTimeInMillis();
		if(dValue<=1000*60) {//分
			return "刚刚";
		}else if(dValue<1000*60*60) {//时
			return (dValue/(1000*60))+"分钟前";
		}else if(dValue<1000*60*60*24) {//天
			return (dValue/(1000*60*60))+"小时前";
		}else if(dValue/10/10/10/60/60/24<30) {//月
			if(dValue/(1000*60*60*24)==1) {
				return "昨天";
			}else if(dValue/(1000*60*60*24)==2) {
				return "前天";
			}else {
				return (dValue/10/10/10/60/60/24)+"天前";
			}
		}else if(dValue/10/10/10/60/60/24<365){
			return (dValue/1000/60/60/24/30)+"个月前";
		}else {
			return (dValue/1000/60/60/24/365)+"年前";
		}
	}

	public static String dateToCompare(Date Onedate,Date TwoDate) {
		Calendar c1 = Calendar.getInstance();
		c1.setTime(Onedate);
		Calendar c2 = Calendar.getInstance();
		c2.setTime(TwoDate);
		Long dValue = c1.getTimeInMillis() - c2.getTimeInMillis();
		if(dValue<=1000*60) {//分
			return "1分钟";
		}else if(dValue<1000*60*60) {//时
			return (dValue/(1000*60))+"分钟";
		}else if(dValue<1000*60*60*24) {//天
			return (dValue/(1000*60*60))+"小时";
		}else if(dValue/10/10/10/60/60/24<30) {//月
			if(dValue/(1000*60*60*24)==1) {
				return "1天";
			}else if(dValue/(1000*60*60*24)==2) {
				return "2天";
			}else {
				return (dValue/10/10/10/60/60/24)+"天";
			}
		}else if(dValue/10/10/10/60/60/24<365){
			return (dValue/1000/60/60/24/30)+"月";
		}else {
			return (dValue/1000/60/60/24/365)+"年";
		}
	}
	/**
	 * 获得指定日期的后一天
	 * @param specifiedDay
	 * @return
	 */
	public static String getSpecifiedDayAfter(String specifiedDay){
		Calendar c = Calendar.getInstance();
		Date date=null;
		try {
			date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day=c.get(Calendar.DATE);
		c.set(Calendar.DATE,day+1);

		String dayAfter=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
		return dayAfter;
	}

	//获取当前周的第一天：
	public static String getFirstDayOfWeek(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(date);
			cal.set(Calendar.DAY_OF_WEEK, 2);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return format.format(cal.getTime());
	}
	//获取当前周最后一天：
	public static String getLastDayOfWeek(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();

		try {
			cal.setTime(date);
			cal.set(Calendar.DAY_OF_WEEK, 1);
			cal.set(Calendar.DATE, cal.get(Calendar.DATE) + 7);
		} catch(Exception e) {
			e.printStackTrace();
		}

		return format.format(cal.getTime());
	}



	//获取当前周的第一天：
	public static Date getFirstOfWeek(Date date) {
		Calendar currentDate = new GregorianCalendar();
		currentDate.setFirstDayOfWeek(Calendar.MONDAY);

		currentDate.set(Calendar.HOUR_OF_DAY, 0);
		currentDate.set(Calendar.MINUTE, 0);
		currentDate.set(Calendar.SECOND, 0);
		currentDate.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);


//		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//		Calendar cal = Calendar.getInstance();
//		try {
//			cal.setTime(date);
//			cal.set(Calendar.DAY_OF_WEEK, 2);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		return currentDate.getTime();
	}
	//获取当前周最后一天：
	public static Date getLastOfWeek(Date date) {
//		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//		Calendar cal = Calendar.getInstance();
//
//		try {
//			cal.setTime(date);
//			cal.set(Calendar.DAY_OF_WEEK, 1);
//			cal.set(Calendar.DATE, cal.get(Calendar.DATE) + 7);
//		} catch(Exception e) {
//			e.printStackTrace();
//		}

		Calendar currentDate = new GregorianCalendar();
		currentDate.setFirstDayOfWeek(Calendar.MONDAY);
		currentDate.set(Calendar.HOUR_OF_DAY, 23);
		currentDate.set(Calendar.MINUTE, 59);
		currentDate.set(Calendar.SECOND, 59);
		currentDate.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		return currentDate.getTime();
	}


	//获取上周的星期一
    public  static String getFirstDayOfYesWeek(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        //设置当前时间为上周的今天
        cal.setTimeInMillis(System.currentTimeMillis());
        cal.add(Calendar.DAY_OF_MONTH, -7);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        String bTime = format.format(cal.getTime());
	    return bTime;
    }
    //获取上周的星期日
    public static String getLastDayOfYesWeek(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        //设置当前时间为上周的今天
        cal.setTimeInMillis(System.currentTimeMillis());
//        cal.add(Calendar.DAY_OF_MONTH, -7);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        String eTime = format.format(cal.getTime());
	    return eTime;
    }



	/**
	 *获取当前月第一天
	 */
	public static String getCurrentMonthFirstDay() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cale = Calendar.getInstance();
		cale.add(Calendar.MONTH, 0);
		cale.set(Calendar.DAY_OF_MONTH, 1);
		return format.format(cale.getTime());
	}

	/**
	 *获取上个月的第一天
	 */
	public static String getYesMonthFirstDay(){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, -1);
		cal.set(Calendar.DAY_OF_MONTH,1);
		String bTime = format.format(cal.getTime());
		return bTime;
	}

	/**
	 *获取上个月的第一天结束时间
	 */
	public static Date getYesMonthFirstDayLast(){
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, -1);
		cal.set(Calendar.DAY_OF_MONTH,1);
		return cal.getTime();
	}

	/**
	 *获取上个月的最后一天--date
	 */
	public static Date getYesMonthLastDayLast() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, 0);
		return cal.getTime();
	}

	/**
	 *获取当前月最后一天：
	 */
	public static String getCurrentMonthLastDay() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cale = Calendar.getInstance();
		cale.add(Calendar.MONTH, 1);
		cale.set(Calendar.DAY_OF_MONTH, 0);
		return format.format(cale.getTime());
	}

	/**
	 *获取上个月的最后一天
	 */
    public static String getYesMonthLastDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, 0);
        String eTime = format.format(cal.getTime());
        return eTime;
    }

	/**
	 *获取某时间的前后几天，num 为负数向前  正向后  前num 天  或后 num天
	 */
    public static Date getPreOrNextDay(Date date,int num) {
        Calendar cal = Calendar.getInstance();
        cal.setTime (date);
		cal.add(Calendar.DATE,num);
        return cal.getTime();
    }

	/**
	 *获取某时间的前后几天，num 为负数向前  正向后  前num 天  或后 num天
	 */
	public static String  getStrPreOrNextDay(Date date,int num) {
		Calendar cal = Calendar.getInstance();
		cal.setTime (date);
		cal.add(Calendar.DATE,num);
		Date dates = cal.getTime();

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");//格式化为年月日

		return sdf.format(dates);
	}

    //前一天
    public static Date getPreDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime (date);
		cal.add(Calendar.DATE,-1);
        return cal.getTime();
    }
    //后一天
    public static Date getNextDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime (date);
		cal.add(Calendar.DATE,1);
        return cal.getTime();
    }


	/**
	 *获取月的第一天
	 */
	public static Date getMonthFirstDay(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime (date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 000);
		cal.set(Calendar.DAY_OF_MONTH,1);
		return cal.getTime ();
	}


	public static Date getpreMonth(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime (date);
		cal.add(Calendar.MONTH, -1);
		return cal.getTime ();
	}

	/**
	 *获取选定时间前几个月或后几个月的月份
	 *  num < 0 向前  num >0 向后
	 * @description:
	 * @param
	 * @return:
	 * @author: xwq
	 * @time: 2020/12/23 20:59
	 */
	public static Date getPreOrNextMonth(Date date,int num){
		Calendar cal = Calendar.getInstance();
		cal.setTime (date);
		cal.add(Calendar.MONTH, num);
		return cal.getTime ();
	}



	/**
	 *
	 * @param nowTime   当前时间
	 * @param startTime	开始时间
	 * @param endTime   结束时间
	 * @return
	 * @author sunran   判断当前时间在时间区间内
	 */
	public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
		if (nowTime.getTime() == startTime.getTime()
				|| nowTime.getTime() == endTime.getTime()) {
			return true;
		}
		Calendar date = Calendar.getInstance();
		date.setTime(nowTime);

		Calendar begin = Calendar.getInstance();
		begin.setTime(startTime);

		Calendar end = Calendar.getInstance();
		end.setTime(endTime);

		if (date.after(begin) && date.before(end)) {
			return true;
		} else {
			return false;
		}
	}



	/**
	 *获取月最后一天："yyyy-MM-dd HH:mm:ss"
	 */
	public static Date getMonthLastDay(Date date) {
		Calendar cal = Calendar.getInstance();
        cal.setTime (date);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		cal.set(Calendar.MILLISECOND, 999);
        cal.add(Calendar.MONTH, 1);
        cal.set(Calendar.DAY_OF_MONTH, 0);

		return cal.getTime ();
	}

	/**
	 *获取月最后一天："yyyy-MM-dd"
	 */
	public static Date getMonthLastsDay(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime (date);
		cal.add(Calendar.MONTH, 1);
		cal.set(Calendar.DAY_OF_MONTH, 0);

		return cal.getTime ();
	}

	//获取当前时间一月前
	public static String getBeforeMonth(){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance();
		//过去一月
		c.setTime(new Date());
		c.add(Calendar.MONTH, -1);
		Date m = c.getTime();
		String mon = format.format(m);
		return mon;
	}



	/**
	 *获取某时间的前后几分钟，num 为负数向前  正向后  前num分钟  或后 num分钟
	 *
	 */
	public static Date getPreOrNextMin(Date date,int num) {
		Calendar cal = Calendar.getInstance();
		cal.setTime (date);
		cal.add(Calendar.MINUTE,num);
		return cal.getTime();
	}


	// month格式：yyyy-MM
	public static List<HashMap> getMonthBetween2(String month) throws ParseException {
		List<HashMap> result = new ArrayList<>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//格式化为年月

		Calendar min = Calendar.getInstance();
		Calendar max = Calendar.getInstance();

		min.setTime(sdf.parse(month + "-01"));

		int year = max.get(Calendar.YEAR);
		int mon = (max.get(Calendar.MONTH) + 1);
		String curMon = year + "-" + (mon < 10 ? "0" + mon : mon);
		if (month.equals(curMon)){
			max.add(Calendar.DATE, 0);
		} else {
			max.setTime(sdf.parse(month + "-01"));
			max.add(Calendar.MONTH, 1);
		}

		while (min.before(max)) {
			HashMap param = new HashMap();
			param.put("date", sdf.format(min.getTime()));
			param.put("day", min.get(Calendar.DATE));
			result.add(param);
			min.add(Calendar.DAY_OF_MONTH, 1);
		}
		return result;
	}

	//获取月的每一天
	public static List<Date> getMonthBetween(Date date) {
		List lDate = new ArrayList();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//格式化为年月

		Date monthFirstDay = getMonthFirstDay(date);
		Calendar min = Calendar.getInstance();
		Calendar max = Calendar.getInstance();

		min.setTime(monthFirstDay);
		max.add(Calendar.DATE, 0);
		//max.add(getMonthHasDays(monthFirstDay), 0);


		while (min.before(max)) {
			lDate.add(sdf.format(min.getTime()));
			min.add(Calendar.DAY_OF_MONTH, 1);
		}
		return lDate;
	}

	/**
	 *
	 *获取连续几个月
	 * @description:
	 * @param
	 * @return:
	 * @author: xwq
	 * @time: 2020/12/23 21:09
	 */
	public static List<String> getMonthBetween(Date minDate, Date maxDate){
		ArrayList<String> result = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");//格式化为年月

		Calendar min = Calendar.getInstance();
		Calendar max = Calendar.getInstance();

		min.setTime(minDate);
		min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

		max.setTime(maxDate);
		max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

		Calendar curr = min;
		while (curr.before(max)) {
			result.add(sdf.format(curr.getTime()));
			curr.add(Calendar.MONTH, 1);
		}

		return result;
	}
	/**
	 *
	 *获取连续几个日
	 * @description:
	 * @param
	 * @return:
	 * @author: xwq
	 * @time: 2020/12/23 21:09
	 */
	public static List<String> getDaysBetween(Date minDate, Date maxDate){

		ArrayList<String> result = new ArrayList<>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");//格式化为年月

		Calendar min = Calendar.getInstance();
		Calendar max = Calendar.getInstance();

		min.setTime(minDate);

		max.setTime(maxDate);

		Calendar curr = min;
		while (curr.before (max)) {
//			System.out.println (curr);
			result.add(sdf.format(curr.getTime()));
			curr.add(Calendar.DAY_OF_MONTH, 1);
		}

		return result;
	}

	public static Date getDateHour(Date date,Integer hour) {
		Calendar cal = Calendar.getInstance();
		cal.setTime (date);
		cal.set(Calendar.HOUR_OF_DAY, hour);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTime();
	}

	/**
	 * 计算版本号
	 * @param dates
	 * @return
	 */
	public static String  getVersion(List<Date> dates) {
		if (dates==null || dates.isEmpty()){
			return null;
		}
		long temp = 0L;
		for(Date date : dates){
			temp += date.getTime();
		}
		String str = Long.toString(temp);

		return str;
	}

	/**
	 *获取本周集合
	 * @param date
	 * @return
	 */
	public static List<Date> getWeekList(Date date){

		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		// 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
		if (1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}
		// System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期
		// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		// 获得当前日期是一个星期的第几天
		int day = cal.get(Calendar.DAY_OF_WEEK);
		// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
		Date imptimeBegin =cal.getTime();
		// System.out.println("所在周星期一的日期：" + imptimeBegin);
		cal.add(Calendar.DATE, 6);
		Date imptimeEnd = cal.getTime();
		// System.out.println("所在周星期日的日期：" + imptimeEnd);

		List lDate = new ArrayList();
		lDate.add(imptimeBegin);
		Calendar calBegin = Calendar.getInstance();
		// 使用给定的 Date 设置此 Calendar 的时间
		calBegin.setTime(imptimeBegin);
		Calendar calEnd = Calendar.getInstance();
		// 使用给定的 Date 设置此 Calendar 的时间
		calEnd.setTime(imptimeEnd);
		// 测试此日期是否在指定日期之后
		while (imptimeEnd.after(calBegin.getTime()))
		{
			// 根据日历的规则，为给定的日历字段添加或减去指定的时间量
			calBegin.add(Calendar.DAY_OF_MONTH, 1);
			lDate.add(calBegin.getTime());
//			param.put("date", sdf.format(min.getTime()));
//			param.put("day", min.get(Calendar.DATE));
		}
		return lDate;

	}

	public static boolean isSunday(Date date){
		Calendar cal=Calendar.getInstance();
		cal.setTime(date);
//		if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY){
//			System.out.println("周六");
//		}
		if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){
			return true;
		}
		return  false;
	}

	public static Map<String,Date> getTimeInterval(Date date) {
		Map<String,Date> map = new HashMap<>();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		// 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
		if (1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}
		// System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期
		// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		// 获得当前日期是一个星期的第几天
		int day = cal.get(Calendar.DAY_OF_WEEK);
		// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
		map.put("BEGIN",cal.getTime());
		// System.out.println("所在周星期一的日期：" + imptimeBegin);
		cal.add(Calendar.DATE, 6);//
		map.put("END",new Date(cal.getTime().getTime()+ 24 * 60 * 60 * 1000 - 1));
		// System.out.println("所在周星期日的日期：" + imptimeEnd);
		return map;
	}

	public static int getWeeks() {
		int week = 0;
		Date today = new Date();
		Calendar c = Calendar.getInstance();
		c.setTime(today);
		int weekday = c.get(Calendar.DAY_OF_WEEK);
		if (weekday == 1) {
			week = 7;
		} else if (weekday == 2) {
			week = 1;
		} else if (weekday == 3) {
			week = 2;
		} else if (weekday == 4) {
			week = 3;
		} else if (weekday == 5) {
			week = 4;
		} else if (weekday == 6) {
			week = 5;
		} else if (weekday == 7) {
			week = 6;
		}
		return week;
	}

	/**
	 *  获取指定时间当前月的第一天
	 * */
	public static Date getMonthFirstDayNew(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH,1);
		calendar.add(Calendar.MONTH,0);
		return calendar.getTime();
	}
	/**
	 *  获取指定时间下个月的第一天
	 * */
	public static Date getNextMonthFirstDayNew(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH,1);
		calendar.add(Calendar.MONTH,1);
		return calendar.getTime();
	}

	/**
	 * 获取两个日期相差的月数
	 */
	public static int getMonthDiff(Date d1, Date d2) {
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(d1);
		c2.setTime(d2);
		int year1 = c1.get(Calendar.YEAR);
		int year2 = c2.get(Calendar.YEAR);
		int month1 = c1.get(Calendar.MONTH);
		int month2 = c2.get(Calendar.MONTH);
		int day1 = c1.get(Calendar.DAY_OF_MONTH);
		int day2 = c2.get(Calendar.DAY_OF_MONTH);
		// 获取年的差值
		int yearInterval = year1 - year2;
		// 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
		if (month1 < month2 || month1 == month2 && day1 < day2) {
			yearInterval--;
		}
		// 获取月数差值
		int monthInterval = (month1 + 12) - month2;
		if (day1 < day2) {
			monthInterval--;
		}
		monthInterval %= 12;
		int monthsDiff = Math.abs(yearInterval * 12 + monthInterval);
		return monthsDiff;
	}

	public static void main(String[] args) {

	/*	String aa = "2021-01-01 00:00:00";
		String bb = "2021-01-01 00:00:00";
		boolean cc = parseDate(aa).after(parseDate(aa));
		int vvv = getMonthDiff(parseDate(bb),parseDate(aa));
		System.out.println(vvv);*/

		String start = MyDateUtils.getStrPreOrNextDay(new Date(),6);

		System.out.println(start);

		String parameter = "2022-12-11";

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Date date = new Date();

		Date starDate = null;
		try {
			starDate = sdf.parse(parameter);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int betweenDay = MyDateUtils.getBetweenDay(starDate,date);

		System.out.println(betweenDay);

	}
}
